$(document).ready(function(){

	var validator = $('#authform').validate({
		rules: {
		  username: {
			required: true,
			email: true,
		  },
		  password: {
			required: true,
		  },
		  recaptcha_response_field: {
			required: true,
		  },
		},
		
		highlight: function(element) {
			var attr = $(element).attr("style")
			if( typeof attr != 'undefined' && attr != false ) {
				$(element).css({'border-color':'#b94a48', 'border-width':'2px'});	
			}
			else {
				$(element).closest('.form-group').addClass('has-error');
			}
		},
		unhighlight: function(element, errorClass, validClass) {
			var attr = $(element).attr("style")
			if( typeof attr != 'undefined' && attr != false ) {
				$(element).css({'border-color':'', 'border-width':'1px'});
			}
			else {
				$(element).closest('.form-group').removeClass('has-error');
			}
		},
		
		submitHandler: function(form) {
			form.submit();
		},
	
	});
		
	$('.login_btn').click(function (){
		validator.resetForm();
		ajax_init();
		$('#authdlg .modal-header.login').show();
		$('#authdlg .modal-header.signup').hide();
		$('#action_id').val('login');
		$('#new_user_alert_info').hide();
		$('#forgot_pw').show();
		$('#recaptcha_widget').hide();
	});
	
	$('.signup_btn').click(function (){
		validator.resetForm();
		ajax_init();
		$('.modal-footer .control-group .progress').hide();
		$('#authdlg .modal-header.login').hide();
		$('#authdlg .modal-header.signup').show();
		$('#action_id').val('signup');
		$('#new_user_alert_info').show();
		$('#forgot_pw').hide();
		$('#recaptcha_widget').show();
	});
	
	function pressEnterToSubmit(e){
		if( e.keyCode == 13 ) {
			$('#auth_form_submit').click();
		}
	}
	
	$('#username_id').keyup( pressEnterToSubmit);
	$('#password_id').keyup( pressEnterToSubmit );
	$('#recaptcha_response_field').keyup( pressEnterToSubmit );
	
	$('#auth_form_submit').click(function(e){
		if( $("#authform").valid() == false ){
			validator.focusInvalid();
			return;
		}
		$('#auth_form_errormsg').hide();
		$('#auth_form_errormsg_captcha').hide();
		
		// auth state : waiting
		ajax_waiting();
		
		data = $('#authform').serialize();
		Dajaxice.users.authjson(resultCallback, {'form':data}, {'error_callback': ajax_error});
		
		e.preventDefault();
	});
	
	$('#authdlg').on('show.bs.modal', function () {
		$('#authdlg').focus();
		$('#username_id').focus();
		if( false == $('#recaptcha_response_field').hasClass('form-control') ) $('#recaptcha_response_field').addClass('form-control');
	})
});

function resultCallback(data) {
		console.log("dajax callback " + data.result);
		 
		// auth state : finish
		ajax_finish();
		
		// 1. success signup
		if( data.result == "success_signup" )
		{
			$('#signup_success_alert').show();
			$('#new_user_alert_info').hide();
			setTimeout(
						function(){
								$('#signup_success_alert').hide();
								window.location = data.redirect_to;
							},
						1500);
			return;
		}
		// 2. success login
		else if( data.result == "success_login" )
		{
			window.location = data.redirect_to;
			return;
		}
		
		// refresh captcha if any error occurs
		$('#recaptcha_reload_btn')[0].click();
		
		// 0.5. captcha failed
		if( data.result == "captcha_fail")
		{
			$('#auth_form_errormsg_captcha').show();
			$('#recaptcha_response_field').css({'border-color':'#b94a48', 'border-width':'2px'});
			$('#recaptcha_response_field').focus();
			
			$('#authdlg').on('hidden.bs.modal', function () {
				$('#auth_form_errormsg_captcha').hide();
				$('#recaptcha_response_field').css({'border-color':'', 'border-width':'1px'});
			});			
		}
		// 1. auth faild  (add error icon and error msg)
		else if( data.result == "invalid_authencate")
		{
			$('#auth_form_errormsg').show();
			$('#authdlg').on('hidden.bs.modal', function () {
				$('#auth_form_errormsg').hide();
			});
		}
}

function ajax_error() {
	ajax_finish();
}

function ajax_init() {
	$('#auth_form_errormsg').hide();
	
	$('#auth_form_errormsg_captcha').hide();
	$('#recaptcha_response_field').removeClass("error");
	
	$('.modal-footer .control-group .controls').show();
	$('.modal-footer .control-group .progress').hide();
}

function ajax_waiting() {
	$('.modal-footer .control-group .controls').hide();
	$('.modal-footer .control-group .progress').show();
}

function ajax_finish() {
	ajax_init();
}